package com.scvtc.service.research.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.scvtc.domain.ResResult;
import com.scvtc.domain.dto.research.work.ResearchWorkDto;
import com.scvtc.domain.entity.research.ResearchWork;
import com.scvtc.domain.vo.PageVo;
import com.scvtc.domain.vo.research.work.ResearchWorkVo;
import com.scvtc.mapper.research.ResearchWorkMapper;
import com.scvtc.service.research.ResearchWorkService;
import com.scvtc.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 科研著作信息(ResearchWork)表服务实现类
 *
 * @author yiFei
 * @since 2022-06-02 16:17:10
 */
@Service("researchWorkService")
public class ResearchWorkServiceImpl extends ServiceImpl<ResearchWorkMapper, ResearchWork> implements ResearchWorkService {
    /**
     * 查询科研著作信息
     *
     * @param current
     * @param size
     * @param researchWorkVo
     * @return
     */
    @Override
    public ResResult<PageVo<List<ResearchWorkVo>>> getAllResearchWork(Long current, Long size, ResearchWorkVo researchWorkVo) {
        // 分页查询
        Page<ResearchWork> page = getBaseMapper().getAllResearchWork(new Page<ResearchWork>(current, size), researchWorkVo);
        // BeanCopy
        List<ResearchWorkVo> researchWorkVoList = BeanCopyUtils.copyBean(page.getRecords(), ResearchWorkVo.class);
        // 返回成功值
        return ResResult.noMsgSuccess(new PageVo<>(researchWorkVoList, page.getTotal()));
    }

    /**
     * 增加科研著作信息
     *
     * @param researchWorkDto
     * @return
     */
    @Override
    public ResResult<?> addResearchWork(ResearchWorkDto researchWorkDto) {
        // 如果考虑后端安全性需要判断教工号是否存在 (代码可参考学生获奖表)
        // Bean拷贝
        ResearchWork researchWork = BeanCopyUtils.copyBean(researchWorkDto, ResearchWork.class);
        // 判断是否成功
        if (save(researchWork)) {
            return ResResult.success(researchWork.getId());
        }
        return ResResult.fail();
    }

    /**
     * 修改科研著作信息
     *
     * @param researchWorkDto
     * @return
     */
    @Override
    public ResResult<?> updateResearchWork(ResearchWorkDto researchWorkDto) {
        // 如果考虑后端安全性需要判断教工号是否存在 (代码可参考学生获奖表)
        // Bean拷贝
        ResearchWork researchWork = BeanCopyUtils.copyBean(researchWorkDto, ResearchWork.class);
        // 判断是否成功
        if (updateById(researchWork)) {
            return ResResult.success();
        }
        return ResResult.fail();
    }

    /**
     * 删除单个科研著作信息
     *
     * @param id
     * @return
     */
    @Override
    public ResResult<?> deleteResearchWork(Long id) {
        // 判断是否成功
        if (removeById(id)) {
            return ResResult.success();
        }
        return ResResult.fail();
    }

    /**
     * 删除多个科研著作信息
     *
     * @param ids
     * @return
     */
    @Override
    public ResResult<?> deleteMultipleResearchWork(Long[] ids) {
        // 判断是否成功
        if (removeByIds(Arrays.asList(ids))) {
            return ResResult.success();
        }
        return ResResult.fail();
    }
}

